/*
COPYRIGHT (C) 2009 EPISERVER AB

THIS FILE IS PART OF VIRTUAL ROLES SAMPLE PACK FOR EPISERVER CMS.
DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

VIRTUAL ROLES SAMPLE PACK FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH VIRTUAL ROLES SAMPLE PACK FOR EPISERVER CMS. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.PlugIn;
using EPiServer.Web.PropertyControls;
using System.Web.Security;
using System.Web;

namespace EPiServer.Research.RoleBasedContent
{
    /// <summary>
    /// Custom PropertyData implementation
    /// </summary>
    [Serializable]
    [PageDefinitionTypePlugIn]
    public class RoleSelector : EPiServer.Core.PropertyLongString
    {
        public string[] Roles
        {
            get
            {
                if (Value == null) return new string[0];
                return this.Value.ToString().Split(',');
            }
            set
            {
                this.Value = string.Join(",", value);
            }
        }

        public bool IsInRoles()
        {
            foreach (string r in Roles)
            {
                if (HttpContext.Current.User.IsInRole(r)) return true;
            }
            return false;
        }


        public override EPiServer.Core.IPropertyControl CreatePropertyControl()
        {
            return new RoleSelectorControl();
        }

    }
}
